<?php

/**
 * 替换或者检测是否有违禁词
 * @author 文林@2011-12-12
 */
class badwords
{

    /**
     * 违禁词缓存文件
     * @var string
     */
    private $replace;
    
    /**
     * BadWords插件映射的文件名
     * @var string
     */
    private $triebin;
    
    /**
     * BadWords插件映射Key
     * @var string
     */
    private $persistkey;
    
    /**
     * 违禁词列表
     * @var array 
     */
    private $badwords;

    /**
     * construct
     * @param string $wordfile
     * @param string $triebin
     * @param string $persistkey 
     */
    public function __construct($replace = array(), $triebin = '', $persistkey = '')
    {
        $this->replace = $replace;
        $this->triebin = $triebin;
        $this->persistkey = $persistkey;
    }

    /**
     * 替换内容中的违禁词
     * @param string $content
     * @return string 
     */
    public function replace(& $content)
    {
        if(!function_exists('badwords_replace'))
        {
            return strtr($message, $this->replace['words']);
        }

        $badwords = self::_get_shared_badwords();

        return badwords_replace($badwords, $content);
    }

    /**
     * 检测内容中是否存在违禁词，如果存在则返回该违禁词
     * @param string $content
     * @return string 
     */
    public function & check(& $content)
    {
        $result = NULL;

        if(!function_exists('badwords_match'))
        {
            foreach($replace['words'] as $key => & $val)
            {
                if(strpos($content, (string) $key) !== FALSE)
                {
                    $result = $key;

                    break;
                }
            }

            return $result;
        }
        else
        {
            $badwords = self::_get_shared_badwords();
            $result = badwords_match($badwords, $content);

            return $result;
        }
    }

    /**
     * 载入Badwords列表资源
     * @return array 
     */
    public function & _get_shared_badwords()
    {
        if(empty($this->badwords))
        {
            $wmtime = $this->replace['uptime'];
            $tmtime = @filemtime($this->triebin);

            if($tmtime === FALSE || ($tmtime !== $wmtime && mt_rand(0, 99) < 10))
            {
                //include($this->wordfile);

                $compiler = badwords_compiler_create(BADWORDS_ENCODING_UTF8, TRUE);
                badwords_compiler_append($compiler, $this->replace['words']);
                unset($this->replace);

                $trie = & badwords_compiler_compile($compiler);
                unset($compiler);

                if($trie)
                {
                    $triebin_tmp = $this->triebin . '-' . getmypid();
                    @file_put_contents($triebin_tmp, $trie);
                    touch($triebin_tmp, $wmtime);
                    rename($triebin_tmp, $this->triebin);

                    unset($trie);
                }
            }

            $this->badwords = badwords_create($this->triebin, $this->persistkey);
        }

        return $this->badwords;
    }

}